package com.sf.fy.uav.applet.order.controller;

import com.sf.fy.uav.applet.order.controller.service.OrderService;
import com.sf.fy.uav.common.awt.RepeatSubmit;
import com.sf.fy.uav.common.request.common.IdRequest;
import com.sf.fy.uav.common.request.order.CheckOrderRequest;
import com.sf.fy.uav.common.request.order.RescuePrepareOrderRequest;
import com.sf.fy.uav.common.request.order.UpdateStatusRequest;
import com.sf.fy.uav.common.response.order.CheckOrderResponse;
import com.sf.fy.uav.common.response.order.OrderListResponse;
import com.sf.fy.uav.common.response.order.PlaceOrderResponse;
import com.sf.fy.uav.common.response.order.RescueOrderResponse;
import com.sf.fy.uav.common.response.user.UserResponse;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.order.controller.OrderInfoRest;
import com.sf.fy.uav.order.rest.OrderRest;
import com.sf.fy.uav.user.rest.UserRest;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author 01430312
 */
@RestController
@RequestMapping("/sys/order/")
public class OrderController {

    @Autowired
    private UserRest userRest;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderRest orderRest;

    @Autowired
    private OrderInfoRest orderInfoRest;

    @GetMapping("get-user-info")
    public Rs<UserResponse> getByUserId() {
        return Rs.success(userRest.getByUserId());
    }


    /**
     * 下单接口
     *
     * @return
     */
    @PostMapping("place-order")
    @RepeatSubmit(interval = 1000, message = "重复提交")
    @Operation(tags = "提交订单", description = "提交订单")
    public Rs<PlaceOrderResponse> placeOrder(@Valid @RequestBody RescuePrepareOrderRequest rescuePrepareOrderRequest) {
        // 调用下单接口
        return Rs.success(orderService.placeOrder(rescuePrepareOrderRequest));
    }


    /**
     * 购物车变更检查订单
     *
     * @return
     */
    @PostMapping("check-order")
    @Operation(tags = "购物车变更检查订单", description = "购物车变更检查订单")
    public Rs<CheckOrderResponse> checkOrder(@RequestBody CheckOrderRequest checkOrderRequest) {
        return Rs.success(orderService.checkOrder(checkOrderRequest));
    }

    @PostMapping("cancel-order")
    @RepeatSubmit(interval = 2000, message = "重复提交")
    @Operation(tags = "取消订单", description = "取消订单")
    @CrossOrigin
    public Rs<Date> cancelOrder(@RequestBody @javax.validation.Valid IdRequest idRequest) {
        return Rs.success(orderInfoRest.cancelOrder(idRequest));
    }

    @GetMapping("get-order-list")
    @Operation(tags = "获取订单列表", description = "获取订单列表")
    public Rs<OrderListResponse> getOrderList(@RequestParam Integer page, @RequestParam Integer size) {
        OrderListResponse orderListResponse = new OrderListResponse();
        List<RescueOrderResponse> rescueOrderResponses = orderService.getOrderList(page, size);
        orderListResponse.setRescueOrderResponses(rescueOrderResponses);
        Long total = orderInfoRest.getSendOrderTotal();
        orderListResponse.setTotal(total);
        return Rs.success(orderListResponse);
    }

    @GetMapping("get-orders-total")
    @Operation(tags = "获取订单总条数", description = "获取订单总条数")
    public Rs<Long> getOrderTotal() {
        return Rs.success(orderInfoRest.getSendOrderTotal());
    }


    @PostMapping("update-status")
    @CrossOrigin
    public Rs<Void> updateStatus(@Valid @RequestBody UpdateStatusRequest updateStatusRequest) {
        orderRest.updateStatus(updateStatusRequest);
        return Rs.success();
    }
}
